﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.QuoteModules
{
    public class RegionQuoteUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<RegionQuoteModel> GetRegionQuoteUpdater()
        {
            NormalDataUpdater<RegionQuoteModel> systemDataUpdater = new NormalDataUpdater<RegionQuoteModel>();
            systemDataUpdater.DataUpdating += RegionValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<RegionQuoteModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<RegionQuoteModel> e)
        {
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("QuoteId") ||
                    e.Insert.PropertyUpdated.Contains("RegionId") )
                {
                    e.Message = "已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }
            }
            string message;
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            QuoteModel model = quoteFetcher.Get(e.Insert.Site, e.Insert.QuoteId, out message);
            if (e.Insert.AutoId == 0 && model != null && model.State != Definition.STATE_NORMAL)
            {
                e.Message = "已审核的表单不能新增";
                e.Cancle = true;
                return;
            }
        }
        private void SingletonValid(object sender, DataValidationEventArgs<RegionQuoteModel> e)
        {
            string message;
            RegionQuoteFetcher titleQuoteFetcher = new RegionQuoteFetcher();
            IList<RegionQuoteModel> models = titleQuoteFetcher.ListByMainId(e.Insert.Site, e.Insert.RegionId, out message, out rows);
            foreach (RegionQuoteModel model in models)
            {
                if (model.AutoId != e.Insert.AutoId && model.RegionId == e.Insert.RegionId)
                {
                    e.Message = "本记录已经存在";
                    e.Cancle = true;
                    return;
                }
            }
        }

        private void RegionValid(object sender, DataValidationEventArgs<RegionQuoteModel> e)
        {
            string message;
            RegionFetcher titleFetcher = new RegionFetcher();
            if (titleFetcher.Get(e.Insert.Site, e.Insert.RegionId, out message) == null)
            {
                e.Message = "区域不存在";
                e.Cancle = true;
                return;
            }
        }

    }
}
